
typedef struct {
    int* _a;
    int _front;
    int _rear;
    int _k;

} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k)
{
    MyCircularQueue* CQ = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    CQ->_a = (int*)malloc(sizeof(int) * (k + 1));//需要多开辟一个空间
    CQ->_front = 0;
    CQ->_rear = 0;
    CQ->_k = k;
    return CQ;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value)//插入元素 
{
    if ((obj->_rear + 1) % (obj->_k + 1) == obj->_front)
    {
        return false;
    }
    else
    {
        obj->_a[obj->_rear] = value;
        obj->_rear++;
        obj->_rear %= (obj->_k + 1);//越界
        return true;
    }
}

bool myCircularQueueDeQueue(MyCircularQueue* obj)//删除元素 
{
    if (obj->_front == obj->_rear)
    {
        return false;
    }
    else {
        obj->_front++;
        obj->_front %= (obj->_k + 1);//越界之后回到起始点
        return true;
    }
}

int myCircularQueueFront(MyCircularQueue* obj)//队首元素
{
    if (obj->_front == obj->_rear)
        return -1;
    else
        return obj->_a[obj->_front];
}

int myCircularQueueRear(MyCircularQueue* obj)//队尾元素
{
    if (obj->_front == obj->_rear)
    {
        return -1;
    }
    else
    {
        int tail = obj->_rear - 1;
        if (tail == -1)
            return obj->_a[obj->_k];
        else
            return obj->_a[tail];
    }
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj)//判断队列空否 
{
    return obj->_front == obj->_rear;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) //判断队列满否
{
    return (obj->_rear + 1) % (obj->_k + 1) == obj->_front;
}

void myCircularQueueFree(MyCircularQueue* obj)//销毁
{
    free(obj->_a);
    free(obj);
}

/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * MyCircularQueue* obj = myCircularQueueCreate(k);
 * bool param_1 = myCircularQueueEnQueue(obj, value);

 * bool param_2 = myCircularQueueDeQueue(obj);

 * int param_3 = myCircularQueueFront(obj);

 * int param_4 = myCircularQueueRear(obj);

 * bool param_5 = myCircularQueueIsEmpty(obj);

 * bool param_6 = myCircularQueueIsFull(obj);

 * myCircularQueueFree(obj);
*/